﻿<script src="~/lib/vue/dist/vue.global.js"></script>
<link href="~/lib/element-plus/dist/index.css" rel="stylesheet" />
<script src="~/lib/element-plus/dist/index.full.js"></script>
<script src="~/lib/axios/dist/axios.js"></script>
<body>
	<div id="app">
		<el-button plain v-on:click="Add()">新增</el-button>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column prop="first_kind_id" label="I级机构编号" width="250"></el-table-column>
			<el-table-column prop="first_kind_name" label="I级机构名称" width="250"></el-table-column>
			<el-table-column prop="first_kind_salary_id" label="薪酬发放责任人编号" width="250"></el-table-column>
			<el-table-column prop="first_kind_sale_id" label="销售责任人编号" width="250"></el-table-column>
			<el-table-column label="变更">
				<template #default="scope">
					<el-button size="small" v-on:click="handleEdit(scope.row)">
						变更
					</el-button>
				</template>
			</el-table-column>
			<el-table-column label="删除">
				<template #default="scope">
					<el-button size="small" v-on:click="handleDelete(scope.row.ffk_id)">
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-dialog v-model="dialogVisibleUp"
				   title="II级机构变更"
				   width="800">
			<span>
				<el-form ref="uruleFormRefUp" :model="uformUp" :rules="urules" label-width="auto" style="max-width: 750px">
					<el-form-item label="I级机构编号" prop="first_kind_id">
						<el-input v-model="uformUp.first_kind_id" />
					</el-form-item>
					<el-form-item label="I级机构名称" prop="first_kind_name">
						<el-input v-model="uformUp.first_kind_name" />
					</el-form-item>
					<el-form-item label="薪酬发放责任人编号" prop="first_kind_salary_id">
						<el-input type="textarea" rows="5" v-model="uformUp.first_kind_salary_id" />
					</el-form-item>
					<el-form-item label="销售责任人编号" prop="first_kind_sale_id">
						<el-input type="textarea" rows="5" v-model="uformUp.first_kind_sale_id" />
					</el-form-item>
					<el-form-item>
						<el-button v-on:click="dialogVisibleUp = false">取消</el-button>
						<el-button type="primary" v-on:click="upOk()">
							变更
						</el-button>
					</el-form-item>
				</el-form>

			</span>

		</el-dialog>

		<el-dialog v-model="dialogVisibleAdd"
				   title="II级机构添加"
				   width="800">
			<span>
				<el-form ref="uruleFormRefAdd" :model="uformAdd" :rules="urules" label-width="auto" style="max-width: 750px">
					<el-form-item label="I级机构编号" prop="first_kind_id">
						<el-input v-model="uformAdd.first_kind_id" />
					</el-form-item>
					<el-form-item label="I级机构名称" prop="first_kind_name">
						<el-input v-model="uformAdd.first_kind_name" />
					</el-form-item>
					<el-form-item label="薪酬发放责任人编号" prop="first_kind_salary_id">
						<el-input type="textarea" rows="5" v-model="uformAdd.first_kind_salary_id" />
					</el-form-item>
					<el-form-item label="销售责任人编号" prop="first_kind_sale_id">
						<el-input type="textarea" rows="5" v-model="uformAdd.first_kind_sale_id" />
					</el-form-item>
					<el-form-item>
						<el-button v-on:click="dialogVisibleAdd = false">取消</el-button>
						<el-button type="primary" v-on:click="AddOk()">
							变更
						</el-button>
					</el-form-item>
				</el-form>

			</span>

		</el-dialog>

	</div>
</body>
<script type="text/javascript">
	const { createApp, ref, reactive, computed, watchEffect } = Vue;
	var app = createApp({
		setup() {
			var tableData = ref([])
			let dialogVisibleUp = ref(false);
			const uruleFormRefUp = ref(null);
			let dialogVisibleAdd = ref(false);
			const uruleFormRefAdd = ref(null);

			const uformUp = reactive({
				ffk_id: '',
				first_kind_name: '',
				first_kind_id: '',
				first_kind_salary_id: '',
				first_kind_sale_id: '',

			})
			const uformAdd = reactive({
				first_kind_name: '',
				first_kind_id: '',
				first_kind_salary_id: '',
				first_kind_sale_id: '',

			})

			function Fill() {
				axios.post('/GetCFFK')
					.then(res => {
						console.info(res.data);
						tableData.value = res.data;
					})
			}
			Fill();

			function handleEdit(row) {
				console.info(row)
				dialogVisibleUp.value = true;
				uformUp.ffk_id = row.ffk_id;
				uformUp.first_kind_name = row.first_kind_name;
				uformUp.first_kind_id = row.first_kind_id;
				uformUp.first_kind_salary_id = row.first_kind_salary_id;
				uformUp.first_kind_sale_id = row.first_kind_sale_id;

			}

			function upOk() {
				uruleFormRefUp.value.validate((valid, fields) => {
					if (valid) {
						axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
						axios.put('/CFFK/UpdateCFFK', {
							ffk_id: uformUp.ffk_id,
							first_kind_name: uformUp.first_kind_name,
							first_kind_id: uformUp.first_kind_id,
							first_kind_salary_id: uformUp.first_kind_salary_id,
							first_kind_sale_id: uformUp.first_kind_sale_id,
						}).then(res => {
							if (res.data) {
								console.info(res.data)
								alert("修改成功")
								Fill();
								dialogVisibleUp.value = false;
							}
						}).catch(err => {
							console.log(err);
						})
					} else {
						console.log('error submit!', fields)
					}
				})

			}
			function handleDelete(ffk_id) {
				console.info(ffk_id)
				if (confirm("确定删除")) {
					axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
					axios.post('/CFFK/DeleteCFFK', {
						id: ffk_id
					}).then(res => {
						if (res.data) {
							Fill();
						}
					}).catch(err => {
						console.log(err);
					})
				}

			}
			function Add() {
				dialogVisibleAdd.value = true;
			}
			function AddOk() {
				axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
				axios.put('/CFFK/AddCFFK', {
					first_kind_name: uformAdd.first_kind_name,
					first_kind_id: uformAdd.first_kind_id,
					first_kind_salary_id: uformAdd.first_kind_salary_id,
					first_kind_sale_id: uformAdd.first_kind_sale_id,
				}).then(res => {
					if (res.data) {
						console.info(res.data)
						alert("添加成功")
						Fill();
						dialogVisibleAdd.value = false;
					}
				}).catch(err => {
					console.log(err);
				})
			}
			return {
				tableData,
				dialogVisibleUp,
				uruleFormRefUp,
				dialogVisibleAdd,
				uruleFormRefAdd,
				uformUp,
				uformAdd,
				handleEdit,
				upOk,
				handleDelete,
				Add,
				AddOk
			}
		}
	})
	app.use(ElementPlus);
	app.mount("#app");
</script>

<style type="text/css">
	body {
		background-color: #FFFFBB;
		
	}

	el-table {
		background-color: #FFFFBB;
	}
</style>
